<template>
  <div class="login-container">
    <!-- <img src="@/assets/images/logingBg.png" alt="" class="bgImg"> -->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <img src="@/assets/images/loginLogo.png" alt="">
        <h3 class="title">黑马面面</h3>
        <h3 class="line" />
        <h3 class="title">用户登录</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: editor@admin.com</span>
        <span> password: 123456</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import shajs from 'sha.js'


export default {
  name: 'Login',
  data() {
    const PasswordRules = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不符合长度'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'editor@admin.com',
        password: '123456'
      },
      loginRules: {
        username: [{
					required: true,
					trigger: 'blur'
				}],
        password: [{
					required: true,
          trigger: 'blur',
          message: '字符应该在 6-12 位之间',
					validator: PasswordRules }]
      },
      loading: false,
      passwordType: 'password', // 控制密码框小眼睛的变化
      redirect: undefined
    }
  },
	computed: {
    ...mapState(['token'])
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
		...mapActions('loginModule', ['asyncLoginToken']),

		// 控制密码框小眼睛的变化
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

		// 点击登录按钮，登录到主页面
   async handleLogin() {
			this.loading = true
			this.$refs.loginForm.validate(async isOk => { // 进行表单验证
				if (isOk) {
					try {
						// 将密码转成HASH值
						const pwd = shajs('sha256').update(this.loginForm.password).digest('hex')
						await this.asyncLoginToken({ username: this.loginForm.username, password: pwd })// 调用vuex中的登录接口，进行登录
						this.$router.push('/')// 跳转到首页
					} catch (err) {
						console.dir(err);
					} finally {
						this.loading = false // finally是，不管是否成功（ try/catch ）最后都会执行的代码体
					}
				}
			})
    }
  }
}
</script>

<style lang="scss">

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
		.bgImg{
			position: absolute;
			right: 30px;
			bottom: 30px;
		}
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;


    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
	// background: linear-gradient(to right,#1493fa, #01c6fa);
	background: url('~@/assets/images/th.jpg') no-repeat;
	background-size: 100% 100% ;

  .login-form {
    position: relative;
    width: 475px;
		height: 390px;
    max-width: 100%;
    padding: 16px 35px 0;
		margin-top: 180px;
		margin-left: 150px;
    overflow: hidden;
		background-color: #f5f5f5;
		opacity: 0.8;
		border-radius: 15px;
  }
	::v-deep .el-input input{
		color: #000;
	}

  .tips {
    font-size: 14px;
    color: #409eff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
	::v-deep .el-form-item{
		background-color: #fff;
	}
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
		overflow: hidden;
		margin-top: 30px;

		img{
			float: left;
			margin-top: 3px;
			margin-right: 10px;

		}
    .title {
			float: left;
      font-size: 26px;
      color: #000;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
		.line{
			float: left;
			margin: 0 10px;
			width: 2px;
			height: 25px;
			background-color: #000;
			margin-top: 3px;
		}
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
